package com.wang.validator;

import com.wang.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @Classname EmailValidator
 * @Description TODO
 * @Date 2024/5/21 9:56
 * @Created by pc
 */
@Component
public class EmailValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        User user = (User)target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors,"email","","邮箱为空");
        if(!user.getEmail().contains("@")){
            errors.rejectValue("email","","邮箱必须包含@");
        }
    }
}
